//
//  WMEPlayerView.m
//  WMEMusic
//
//  Created by 恩叔 on 16/4/29.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEPlayerView.h"
#import "WMESongListModel.h"
@interface WMEPlayerView ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIImageView *cover;
@property (nonatomic, strong)UITableView *songList;
@property (nonatomic, strong)UITableView *lyTableView;
@end

@implementation WMEPlayerView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:_scrollView];
        _scrollView.contentSize = CGSizeMake(self.frame.size.width * 3, self.frame.size.height);
        //整页翻动
        _scrollView.pagingEnabled = YES;
        //边缘弹动
        _scrollView.bounces = YES;
        _scrollView.delegate = self;
        
        self.songList = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.frame.size.height)];
        _songList.delegate = self;
        _songList.dataSource = self;
//        _songList.backgroundColor = [UIColor redColor];
        [self.scrollView addSubview:_songList];
        
//        self.cover = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH + 10, 10, WIDTH - 20, WIDTH - 20)];
//        //裁剪边界
//        _cover.layer.masksToBounds = YES;
//        //设置圆角
//        _cover.layer.cornerRadius = (WIDTH - 20)/ 2.0f;
//        //设置边界颜色
//        _cover.layer.borderColor = [[UIColor whiteColor] CGColor];
//        //设置边界宽度
//        _cover.layer.borderWidth = 3.0f;
//        _cover.backgroundColor = [UIColor orangeColor];
//        _cover.userInteractionEnabled = YES;
//        [self.scrollView addSubview:_cover];
        
        self.lyTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH * 2, 0, WIDTH, self.frame.size.height)];
        _lyTableView.dataSource = self;
        _lyTableView.delegate = self;
        _lyTableView.backgroundColor = [UIColor yellowColor];
        [self.scrollView addSubview:_lyTableView];
        [_songList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"songCell"];
        [_lyTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"lyCell"];
        _scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    return self;
}
- (void)setDic:(NSDictionary *)dic
{
    if (_dic != dic) {
        _dic = dic;
//        if (_dic[@"album_500_500"] != nil) {
//            [_cover sd_setImageWithURL:[NSURL URLWithString:_dic[@"album_500_500"]]];
//        }else{
            [_cover sd_setImageWithURL:[NSURL URLWithString:_dic[@"artist_500_500"]]];
//        }
        
    }
}
- (void)setSongArr:(NSMutableArray *)songArr
{
    if (_songArr != songArr) {
        _songArr = songArr;
        [_songList reloadData];
    }
}
- (void)setStr:(NSString *)str
{
    if (_str != str) {
        _str = str;
        NSLog(@"%@", _str);
        //歌词和时间数组
        NSArray *arr = [str componentsSeparatedByString:@"\n"];
        //歌词时间数组
        NSMutableArray *timerMutableArray = [NSMutableArray array];
        //歌词数组
        NSMutableArray *lysicMutableArray = [NSMutableArray array];
        
        for (NSString *string in arr) {
            
            //拆分时间和歌词
            NSArray *timeAndLyric = [string componentsSeparatedByString:@"]"];
            //拆分时间
            NSArray *timerArray = [timeAndLyric.firstObject componentsSeparatedByString:@"["];
            //添加时间
            [timerMutableArray addObject:timerArray.lastObject];
            //添加歌词
            [lysicMutableArray addObject:timeAndLyric.lastObject];
        }
    }
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView == _scrollView) {
//        
//    }
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _songList) {
        return _songArr.count;
    }else if (tableView == _lyTableView) {
        return 20;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _songList) {
        if (_songArr.count) {
            WMESongListModel *model = _songArr[indexPath.row];
            UITableViewCell *songCell = [_songList dequeueReusableCellWithIdentifier:@"songCell" forIndexPath:indexPath];
            songCell.textLabel.text = model.title;
            return songCell;
        }
        return nil;
    }else if (tableView == _lyTableView) {
        UITableViewCell *lyCell = [_lyTableView dequeueReusableCellWithIdentifier:@"lyCell" forIndexPath:indexPath];
        return lyCell;
    }else {
        return nil;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
